from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

from myapp.models import User


class CheckUsername(APIView):
    def get(self, request, username):
        rest = User.objects.filter(username=username).count()
        if rest > 0:
            return Response({'msg': '该用户已注册', 'code': '400'})
        return Response({'code': '200'})

class CheckMobile(APIView):
    def get(self, request, mobile):
        rest = User.objects.filter(mobile=mobile).count()
        if rest > 0:
            return Response({'msg': '该手机号已注册', 'code': '400'})
        return Response({'code': '200'})

class UserRegister(APIView):
    def post(self, request):
        username1 = request.data.get('userName')
        pwd = request.data.get('pwd')
        mobile = request.data.get('mobile')
        agree = request.data.get('agree')
        if not all([username1, pwd, mobile, agree]):
            return Response({'msg': '请完善注册用户信息', 'code': '400'})
        if not agree:
            return Response({'msg': '请勾选协议'})
        User.objects.create_user(username=username1, password=pwd, mobile=mobile, agree_rule=agree)
        return Response({'msg': '注册成功', 'code': '200'})






